﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace sign_recognition
{
    public partial class TrainningForm : Form
    {
        public FuzzyART fuzzyART;
        public bool firstExample=true;
        public TrainningForm()
        {
            InitializeComponent();
        }

        private void loadImageButton_Click(object sender, EventArgs e)
        {
            //this.pbHuella.Image = Bitmap.FromFile(dirImagen);

            openFileDialog.Filter = "bmp files (*.bmp)|*.bmp";
            openFileDialog.ShowDialog();
            if (null==openFileDialog.FileName)
            {
                return;
            }
            String imageDir = openFileDialog.FileName;

            Bitmap a = new Bitmap(imageDir);
            ToolBox tool = new ToolBox();
            this.pictureBoxSign.Image = tool.MakeGrayscale(a);
                //this.MakeGrayscale(Bitmap.FromFile(imageDir));
        
            Console.WriteLine(pictureBoxSign.Width+"  - "+pictureBoxSign.Height);
        }
        public Double[] getInput() {
            int index = 0;
            int total = pictureBoxSign.Image.Height * pictureBoxSign.Image.Width;
            double[] entrada = new double[total];

            for (int y = 0; y < pictureBoxSign.Image.Height; y++)
            {
                for (int x = 0; x < pictureBoxSign.Image.Width; x++)
                {
                    entrada[index] = (((Bitmap)pictureBoxSign.Image).GetPixel(x, y).R) / 255.0;
                    index++;
                }
            }
            return entrada;
        }
        public Double[] getInput(int index)
        {

            Double[] input=null;
            switch (index)
            {
                case 1:
                input = new Double[4];
                input[0]=0.1;
                input[1]=0.1;
                input[2]=0.9;
                input[3]=0.9;
                break;

                case 2:
                input = new Double[4];
                input[0] = 0.2;
                input[1] = 0.1;
                input[2] = 0.8;
                input[3] = 0.9;
                break;

                case 3:
                input = new Double[4];
                input[0] = 0.3;
                input[1] = 0.4;
                input[2] = 0.6;
                input[3] = 0.4;
                break;

                case 4:
                input = new Double[4];
                input[0] = 0.3;
                input[1] = 0.4;
                input[2] = 0.6;
                input[3] = 0.7;
                break;

                default:
                break;
            }

            return input;
        }
        private void Trainbutton_Click(object sender, EventArgs e)
        {
            /*if (this.firstExample)
            {




               // fuzzyART = new FuzzyART(this.getInput(), false, 0.1, 0.9, 1, Int32.Parse(this.comboBoxLetra.SelectedItem.ToString()));
                firstExample = false;
            }
            else {
            */


                //fuzzyART.currentOutput = Int32.Parse(this.comboBoxLetra.SelectedItem.ToString());
                int categorySelected=Int32.Parse(this.comboBoxLetra.SelectedItem.ToString());

                //fuzzyART.propagateNewInput(this.getInput(categorySelected), 1);
            
         //   }







        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void ButtonClasify_Click(object sender, EventArgs e)
        {


            //MessageBox.Show(fuzzyART.clasify(this.getInput()).ToString());
            MessageBox.Show(fuzzyART.clasify(this.getInput(Int32.Parse(this.comboBoxLetra.Text))).ToString());
        }

        private void TrainningForm_Load(object sender, EventArgs e)
        {
            comboBoxLetra.SelectedIndex = 0;
            //fuzzyART = new FuzzyART(this.getInput(), false, 0.1, 0.9, 1);
            fuzzyART = new FuzzyART(4, true, 0.1, 0.9, 1);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            if (saveFileDialog1.FileName != null)
            {
                this.fuzzyART.saveWeights(saveFileDialog1.FileName);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();
            if (null!=openFileDialog.FileName)
            {
                this.fuzzyART.loadWeights(openFileDialog.FileName);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            int categorySelected = Int32.Parse(this.comboBoxLetra.SelectedItem.ToString());
            //fuzzyART.propagateNewInput(this.getInput(), categorySelected);
           // fuzzyART.propagateNewInput(this.getInput(categorySelected), 2);
        }
      /*  public  Bitmap MakeGrayscale(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            //create the grayscale ColorMatrix
            ColorMatrix colorMatrix = new ColorMatrix(
                new float[][]{
                    new float[] {.3f, .3f, .3f, 0, 0},
                    new float[] {.59f, .59f, .59f, 0, 0},
                    new float[] {.11f, .11f, .11f, 0, 0},
                    new float[] {0, 0, 0, 1, 0},
                    new float[] {0, 0, 0, 0, 1}
                });

            //create some image attributes
            ImageAttributes attributes = new ImageAttributes();

            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);

            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            //dispose the Graphics object
            g.Dispose();
            return newBitmap;
        }*/
    }
}
